const request = require("request")
var config = require('../adminApi/config');
var wechat = require('../adminApi/wechat');
var wechatApp = new wechat(config);



// 增删改查
const database = (query, type) => {
    return new Promise(async function   (resolve, reject) {
        let access_token = await wechatApp.getAccessToken().then(function (data) {
            return data;
        });
        var querydata = {};
        if (type === 'cloundFunction') {
            querydata = {
                env: config.envId,
                name: query.name,
                POSTBODY:query.POSTBODY,
            }
        } else if (type === 'upload') {
            querydata = {
                env: config.envId,
                path: query.path,
            }
        } else {
            querydata = {
                env: config.envId,
                query,
            }
        }
        let url = '';
        if (type === 'get') {
            url = 'https://api.weixin.qq.com/tcb/databasequery'
        } else if (type === 'add') {
            url = 'https://api.weixin.qq.com/tcb/databaseadd'
        } else if (type === 'del') {
            url = 'https://api.weixin.qq.com/tcb/databasedelete'
        } else if (type === 'update') {
            url = 'https://api.weixin.qq.com/tcb/databaseupdate';
        } else if (type === 'upload') {
            url = 'https://api.weixin.qq.com/tcb/uploadfile'
        } else if (type === 'delete') {
            url = 'https://api.weixin.qq.com/tcb/databasedelete'
        } else if (type === 'aggregate') {
            url = 'https://api.weixin.qq.com/tcb/databaseaggregate'
        } else if (type === 'cloundFunction') {
            url = `https://api.weixin.qq.com/tcb/invokecloudfunction?access_token=${access_token}&env=${config.envId}&name=${query.name}`
        }


        request({
            url: type === 'cloundFunction' ? url : url + '?access_token=' + ( access_token),
            method: 'post',
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(querydata),
        }, (error, response, body) => {
            if (type === 'upload') {
                resolve(JSON.parse(body));
                return;
            } else if (type === 'cloundFunction') {
                resolve(JSON.parse(body));
            }
            let { data, pager } = JSON.parse(body);
            if (data) {
                let newData = [];
                for (let i = 0, j = data.length; i < j; i++) {
                    newData.push(JSON.parse(data[i]));
                }
                if (error) {
                    reject(error)
                } else {
                    if (pager) {
                        pager.list = newData;
                        resolve(pager)
                    } else {
                        resolve(newData)
                    }
                }
            } else {
                if (error) {
                    reject(error)
                } else {
                    resolve(data)
                }
            }


        })
    })
}





module.exports = {
    database,
}